package com.ycsj.goldmedalnewconcept.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.bean.CourseData;
import com.ycsj.goldmedalnewconcept.bean.CourseInfo;
import com.ycsj.goldmedalnewconcept.bean.CourseInfo.Data;
import com.ycsj.goldmedalnewconcept.constant.Constant;
import com.ycsj.goldmedalnewconcept.fragment.ClassInfoFragment;
import com.ycsj.goldmedalnewconcept.fragment.CourseFragment;
import com.ycsj.goldmedalnewconcept.fragment.DiyRedPackageFragment;
import com.ycsj.goldmedalnewconcept.fragment.RedPackageFragment;
import com.ycsj.goldmedalnewconcept.fragment.TeacherInfoFragment;
import com.ycsj.goldmedalnewconcept.utils.IndicatorManager;
import com.ycsj.goldmedalnewconcept.utils.SPUtil;
import com.ycsj.goldmedalnewconcept.view.magicindicator.MagicIndicator;
import com.ycsj.goldmedalnewconcept.view.supervp.SuperViewPager;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 	新版发红包（校长端）
 */
@SuppressLint("NewApi")
public class CreatRedPackageActivity extends FragmentActivity {

	// 返回
	LinearLayout llBack;
	
	// 标题
	TextView tvTitle;
	
	//皮肤
	TextView tv_pifu;

	// 导航条
	MagicIndicator indicator;

	// VP
	SuperViewPager superVp;


	// Tab的标题集合
	private String[] mTites = { "拼手气红包", "定制红包" };

	// 教室classnum
	private String classNum;

	// ViewPager的Framgent集合
	private List<Fragment> mFramgents;
	private VpAdapter adapter;
	private Intent intent;
	private String account;
	private String user_id;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_creatredpackage);
		intent = getIntent();
		user_id = intent.getStringExtra("user_id");

		account = SPUtil.getString(this, "account", "");
		
		// 设置6.0以后沉浸状体栏的问题
		setBar();

		initView();

		initListener();

		getIntentData();

		initMag();

		initData();

	}

	/**
	 * 初始化View方法
	 */
	private void initView() {
		llBack = (LinearLayout) findViewById(R.id.ll_back);
		indicator = (MagicIndicator) findViewById(R.id.mag);
		superVp = (SuperViewPager) findViewById(R.id.vp);
		tv_pifu = (TextView) findViewById(R.id.tv_pifu);
	}

	
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		
		SPUtil.putString(this, "imagePath", "");
    	SPUtil.putString(this, "themeId", "");
    	SPUtil.putString(this, "diyJson", "");
		super.onDestroy();
	}
	
	
	/**
	 * 设置监听的方法
	 */
	private void initListener() {
		
		tv_pifu.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(CreatRedPackageActivity.this, RedPacageTemplateActivity.class);
				startActivity(intent);
			}
		});
		
		llBack.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				finish();
			}
		});
	}

	/**
	 * 获取页面传递过来的值
	 */
	public void getIntentData() {
		
		

	}

	/**
	 * 初始化导航条的方法
	 */
	private void initMag() {
		IndicatorManager.setIndicator(this, 0, indicator, superVp, mTites,
				getResources().getColor(R.color.white), getResources()
						.getColor(R.color.white), null);
	}

	/**
	 * 初始化页面数据,这列需要请求服务器来获取数据
	 */
	private void initData() {
		mFramgents = new ArrayList<Fragment>();

		RedPackageFragment fragment1 = new RedPackageFragment();
		DiyRedPackageFragment fragment2 = new DiyRedPackageFragment();
//
//		Bundle bundle = new Bundle();
//        bundle.putSerializable("user_id",user_id);
//        Bundle bundle2 = new Bundle();
//        bundle2.putSerializable("user_id",user_id);
//        fragment1.setArguments(bundle);
//        fragment2.setArguments(bundle2);
//        
//        fragment1.setArguments(bundle);
//        fragment2.setArguments(bundle2);
		mFramgents.add(fragment1);
		mFramgents.add(fragment2);
		
		// 初始化ViewPager控件
		initVp();
	}


	/**
	 * 设置ViewPager
	 */
	private void initVp() {
		adapter = new VpAdapter(getSupportFragmentManager());
		superVp.setAdapter(adapter);
		superVp.setOffscreenPageLimit(mFramgents.size());
	}

	/**
	 * ViewPager的适配器
	 */
	public class VpAdapter extends FragmentStatePagerAdapter {

		public VpAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int position) {
			return mFramgents.get(position);
		}

		@Override
		public int getCount() {
			return mFramgents.size();
		}
	}

	/**
	 * 设置沉浸状态栏的方法,需要在setContentView()之后调用
	 */
	private void setBar() {
		// 兼容5.0+，状态栏设置透明
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
			Window window = getWindow();
			window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
			window.getDecorView().setSystemUiVisibility(
					View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
							| View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
			window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
			window.setStatusBarColor(Color.TRANSPARENT);
		} else
		// 为了沉浸式状态栏 4.4以上才有
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
			getWindow().addFlags(
					WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
		}
	}

}
